/**
 * Bestpay.com.cn Inc.
 * Copyright (c) 2011-2020 All Rights Reserved.
 */
package com.suxiaoxiao.springbootlearn.designpattern.statepattern.goodcode.pojo;

import com.suxiaoxiao.springbootlearn.designpattern.statepattern.goodcode.State;
import com.suxiaoxiao.springbootlearn.designpattern.statepattern.goodcode.rmi.server.GumballMachineRemote;
import com.suxiaoxiao.springbootlearn.designpattern.statepattern.goodcode.stateimpl.*;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

/**
 * desc
 *
 * @author zhaorongzhang
 * @version GumballMachine.java, v 0.1 2020/1/9 9:30 下午 zhaorongzhang Exp $$
 */
@Getter
@Setter
@ToString
public class GumballMachine extends UnicastRemoteObject implements GumballMachineRemote {

    /**
     * 售尽
     */
    public State soldOutState;
    /**
     * 没投币
     */
    public State noQuarterState;
    /**
     * 已投币
     */
    public State hasQuarterState;
    /**
     * 售出糖果
     */
    public State soldState;
    /**
     * 双倍奖励
     */
    public State winnerState;

    /**
     * 当前状态
     */
    State state = soldOutState;

    /**
     * 机器中糖果的数量
     */
    int count = 0;

    /**
     * 位置
     */
    String location;

    public GumballMachine(int numberGumballs) throws RemoteException {
        this(null, numberGumballs);
    }

    public GumballMachine(String location, int numberGumballs) throws RemoteException {
        soldOutState = new SoldOutState(this);
        noQuarterState = new NoQuarterState(this);
        hasQuarterState = new HasQuarterState(this);
        soldState = new SoldState(this);
        winnerState = new WinnerState(this);
        this.count = numberGumballs;
        this.location = location;
        if (numberGumballs > 0) {
            state = noQuarterState;
        }
    }

    public void insertQuarter() {
        state.insertQuarter();
    }

    public void ejectQuarter() {
        state.ejectQuarter();
    }

    public void turnCrank() {
        state.turnCrank();
        state.dispense();
    }

    public void refill(int count) {
        state.refill(count);
    }

    public void setState(State state) {
        this.state = state;
    }

    /**
     * 糖果数量 -1
     */
    public void releaseBall() {
        System.out.println("糖果数量 -1");
        if (count != 0) {
            count = count - 1;
        }
    }

}